/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.rafkind.resrev.web.page.component;

import com.rafkind.resrev.dao.ReservationDao;
import com.rafkind.resrev.dao.ResourceDao;
import com.rafkind.resrev.data.Condition;
import com.rafkind.resrev.data.Reservation;
import com.rafkind.resrev.data.Resource;
import java.util.Arrays;
import java.util.Date;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.spring.injection.annot.SpringBean;

/**
 *
 * @author Fictional Hero
 */
public class FinalConditionPanel extends Panel {
  
  @SpringBean
  private ReservationDao reservationDao;
  
  @SpringBean
  private ResourceDao resourceDao;

  public FinalConditionPanel(String id, final IModel<Reservation> model) {
    super(id, model);
    
    Form conditionForm = new Form("conditionForm");
    
    conditionForm.add(new DropDownChoice("conditionChoice",
      new PropertyModel(model, "afterCondition"),
      Arrays.asList(Condition.values())));
    
    conditionForm.add(new Button("saveCondition"){
      @Override
      public void onSubmit() {
        Reservation r = model.getObject();
        
        reservationDao.saveReservation(r);
        
        if (r.getResource().getLastConditionUpdate().before(new Date())) {
          Resource res = r.getResource();
          res.setCurrentCondition(r.getAfterCondition());
          res.setLastConditionUpdate(new Date());
          resourceDao.saveResource(res);
        }
      }
    });
    
    add(conditionForm);
  }  
}